package krakow.ae.knp.wikiboard.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;

import krakow.ae.knp.wikiboard.gui.tools.DrawingMode;

/**
 *
 * A class that represents a Graphical User Interface
 * that includes Board and Buttons.
 *
 * @author Kolo Naukowe Programistow AE w Krakowie
 */
public class GUI extends JPanel {
    
    private Board board = null;
    private JToggleButton
            btDrawStraightLine,
            btDrawLine,
            btDrawText,
            btDrawEllipse,
            btDrawRectangle,
            btFill,
            btSelect,
            btRotate,
            btResize;
    private JButton btUndo, btRedo, btClearAll, btDelete;
    private JButton btBgColor, btFgColor;
    private ButtonGroup group;
    /**
     * Creates a new instance of GUI
     */
    
    public GUI() 
    {
        addComponents();
        layoutComponents();
    }
    
    private void addComponents()
    {
        board = new Board(new Dimension(600, 400));
        
        btDrawStraightLine = new JToggleButton();
        btDrawStraightLine.setIcon(createImageIcon("images/buttons/btDrawStraightLine.gif"));
        btDrawStraightLine.setToolTipText("Rysuj prostą");
        btDrawStraightLine.setPreferredSize(new Dimension(30, 30));
        
        btDrawStraightLine.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                board.setDrawingMode(DrawingMode.STRAIGHT_LINE);
                board.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
            }
        });
        
        btDrawLine = new JToggleButton();
        btDrawLine.setIcon(createImageIcon("images/buttons/btDrawLine.gif"));
        btDrawLine.setToolTipText("Rysuj krzywą");
        btDrawLine.setPreferredSize(new Dimension(30, 30));
        
        btDrawLine.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                board.setDrawingMode(DrawingMode.LINE);
                board.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
            }
        });
        
        btDrawText = new JToggleButton();
        btDrawText.setIcon(createImageIcon("images/buttons/btDrawText.gif"));
        btDrawText.setToolTipText("Pisz tekst");
        btDrawText.setPreferredSize(new Dimension(30, 30));
        btDrawText.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                board.setDrawingMode(DrawingMode.TEXT);
                board.setCursor(new Cursor(Cursor.TEXT_CURSOR));
            }
        });
        
        btDrawEllipse = new JToggleButton();
        btDrawEllipse.setIcon(createImageIcon("images/buttons/btDrawEllipse.gif"));
        btDrawEllipse.setToolTipText("Rysuj elipsę");
        btDrawEllipse.setPreferredSize(new Dimension(30, 30));
        btDrawEllipse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                board.setDrawingMode(DrawingMode.ELLIPSE);
                board.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
            }
        });
        
        btDrawRectangle = new JToggleButton();
        btDrawRectangle.setIcon(createImageIcon("images/buttons/btDrawRectangle.gif"));
        btDrawRectangle.setToolTipText("Rysuj prostokąt");
        btDrawRectangle.setPreferredSize(new Dimension(30, 30));
        btDrawRectangle.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                board.setDrawingMode(DrawingMode.RECTANGLE);
                board.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
            }
        });
        
        btFill = new JToggleButton();
        btFill.setIcon(createImageIcon("images/buttons/btFill.gif"));
        btFill.setToolTipText("Wypełnij kolorem");
        btFill.setPreferredSize(new Dimension(30, 30));
        
        btSelect = new JToggleButton();
        btSelect.setToolTipText("Zaznacz");
        btSelect.setIcon(createImageIcon("images/buttons/btSelect.gif"));
        btSelect.setPreferredSize(new Dimension(30, 30));
        btSelect.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                board.setDrawingMode(DrawingMode.SELECT);
                board.setCursor(new Cursor(Cursor.HAND_CURSOR));
            }
        });
        
        btDelete = new JButton();
        btDelete.setIcon(createImageIcon("images/buttons/btDelete.gif"));
        btDelete.setToolTipText("Skasuj");
        btDelete.setPreferredSize(new Dimension(30, 30));
        
        btRotate = new JToggleButton();
        btRotate.setIcon(createImageIcon("images/buttons/btRotate.gif"));
        btRotate.setToolTipText("Obróć");
        btRotate.setPreferredSize(new Dimension(30, 30));
        
        btResize = new JToggleButton();
        btResize.setIcon(createImageIcon("images/buttons/btResize.gif"));
        btResize.setToolTipText("Zmień rozmiar");
        btResize.setPreferredSize(new Dimension(30, 30));
        
        btUndo = new JButton();
        btUndo.setIcon(createImageIcon("images/buttons/btUndo.gif"));
        btUndo.setToolTipText("Cofnij");
        btUndo.setPreferredSize(new Dimension(30, 30));
        btUndo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                board.undo();
            }
        });
        
        btRedo = new JButton();
        btRedo.setIcon(createImageIcon("images/buttons/btRedo.gif"));
        btRedo.setToolTipText("Powtórz");
        btRedo.setPreferredSize(new Dimension(30, 30));
        btRedo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                board.redo();
            }
        });
        
        btClearAll = new JButton("Wyczyść");
        btClearAll.setPreferredSize(new Dimension(90, 30));
        btClearAll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                board.clearDrawing();
            }
        });
        
        btBgColor = new JButton();
        btBgColor.setPreferredSize(new Dimension(20, 20));
        btBgColor.setBackground(Board.bgColor);
        btBgColor.setOpaque(true);
        btBgColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                changeBgColor();
            }
        });
        
        btFgColor = new JButton();
        btFgColor.setPreferredSize(new Dimension(20, 20));
        btFgColor.setBackground(Board.fgColor);
        btFgColor.setOpaque(true);
        btFgColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                changeFgColor();
            }
        });
        
        
    }
    
    private void layoutComponents()
    {
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.NONE;
        
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 6;
        c.insets = new Insets (5,5,5,15);
        add(board, c);
        
        c.insets = new Insets (1,1,1,1);
        c.gridx = 1;
        c.gridy = 0;
        c.gridheight = 1;
        
        add(btDrawStraightLine, c);
        
        c.gridx = 2;
        add(btDrawLine, c);
        
        c.gridx = 3;
        add(btDrawText, c);
        
        c.gridx = 1;
        c.gridy = 1;
        c.insets = new Insets(1, 1, 10, 1);
        add(btDrawEllipse, c);
        
        c.gridx = 2;
        add(btDrawRectangle, c);
        
        c.gridx = 3;
        add(btFill, c);
        
        c.gridx = 1;
        c.gridy = 2;
        c.insets = new Insets(1, 1, 1, 1);
        add(btSelect, c);
        
        c.gridx = 2;
        add(btDelete, c);
        
        c.gridx = 3;
        add(btRotate, c);
        
        c.gridx = 1;
        c.gridy = 3;
        c.insets = new Insets(1, 1, 10, 1);
        add(btResize, c);
        
        c.gridx = 2;
        add(btUndo, c);
        
        c.gridx = 3;
        add(btRedo, c);
        
        c.gridx = 1;
        c.gridy = 4;
        c.gridwidth = 3;
        c.anchor = GridBagConstraints.NORTH;
        add(btClearAll, c);
        
        addButtonGroup();
        
        c.gridwidth = 1;
        c.gridy = 5;
        add(btBgColor, c);
        c.gridx = 2;
        add(btFgColor, c);
               
    }
    
    private void addButtonGroup()
    {
        group = new ButtonGroup();
        
        Component[] components = getComponents();
        for (Component elem : components)
        {
            if (elem instanceof AbstractButton)
                group.add((AbstractButton)elem);
        }
    }
    
    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = GUI.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    
    private void changeBgColor()
    {
        Color c = JColorChooser.showDialog(this, "Wybierz kolor tła", Board.bgColor);
        if (c != null)
        {
            btBgColor.setBackground(c);
            Board.bgColor = c;
        }
    }
    
    private void changeFgColor()
    {
        Color c = JColorChooser.showDialog(this, "Wybierz kolor rysowania", Board.fgColor);
        if (c != null)
        {
            btFgColor.setBackground(c);
            Board.fgColor = c;
        }
    }
     
}
